﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using XFControls.XFPanels.XFPanelItems;

namespace XFControls.XFPanels
{
    public enum XFPanelHeaderState
    {
        Enabled,
        Disabled
    }
    public partial class XFPanelHeader : XFPanelBase
    {
        public XFPanelContainer MainPanelContainer { get; set; }
        public Color BackgroundColor { get; set; }
        public string CenterText { get; set; }
        public Color CenterTextColor { get; set; }
        public IXFButton LeftButton { get; set; }
        public IXFButton RightButton { get; set; }

        private XFPanelHeaderState _curState;

        public XFPanelHeader()
        {
            CenterTextColor = Color.White;
            InitializeComponent();
            BackgroundColor = Color.Black;
        }

        public void DisableHeader(bool disable)
        {
            if (disable)
            {
                _curState = XFPanelHeaderState.Disabled;
            }
            else
            {
                _curState = XFPanelHeaderState.Enabled;
            }
        }

        public override void PanelPaint(Graphics g)
        {
            using (SolidBrush b = new SolidBrush(BackgroundColor))
                g.FillRectangle(b, 0, 0, Width, Height);

            this.DrawUI("header", g, new Rectangle(0, 0, Width, Height));

            if (string.IsNullOrEmpty(CenterText))
                return;

            int xmid = Width / 2;
            int ymid = Height / 2;

            using (Font f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold))
            {
                xmid -= (int)(g.MeasureString(CenterText, f).Width / 2);
                ymid -= (int)(g.MeasureString(CenterText, f).Height / 2);
                g.DrawString(CenterText, f, new SolidBrush(CenterTextColor), xmid, ymid);
            }

            if (LeftButton != null)
                LeftButton.ButtonPaint(g, 5, 5);
            if (RightButton != null)
                RightButton.ButtonPaint(g, Width - 5 - RightButton.Width, 5);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (_curState == XFPanelHeaderState.Disabled)
                return;

            if (LeftButton != null && e.X < LeftButton.Width + 5)
            {
                LeftButton.Click();
            }

            if (RightButton != null && e.X > Width - 5 - RightButton.Width)
            {
                RightButton.Click();
            }
        }
    }
}
